package org.gameg.web.biz;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.gameg.Api.Service.ApiService;
import org.gameg.Api.entity.SysUserOperate;
import org.gameg.Api.util.ResponseUtil;
import org.gameg.ShiroRealm;
import org.gameg.mapper.biz.SysUserMapper;
import org.gameg.model.biz.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.ibase4j.core.util.WebUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/session")
public class LoginContoller {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ApiService apiService;
    @Autowired
    private ShiroRealm shiroRealm;

    public SysUser getUser(SysUser user) {
        return sysUserMapper.selectOne(user);
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(HttpServletRequest request, SysUser sysUser) throws Exception {
        Map<String, Object> map = new HashMap<>();
        SysUser user = WebUtil.getParameter(request, SysUser.class);
        Map<String, Object> res = new HashMap<>();
        String pwd = user.getPassword();
        SysUser param = new SysUser();
        param.setUserName(user.getUserName());
        user = getUser(param);
        if (user.getId() == null || user.getId().equals("")) {
            map.put("code", 200);
            map.put("msg", "当前用户不存在");
            map.put("isSuccess", false);
        } else {
            if (SysUserOperate.getUser()==null) {
                SysUser token = new SysUser(pwd,user.getUserName());
                boolean info = shiroRealm.login(token);
                if (info) {
                    SysUserOperate.setUser(user, SysUserOperate.getsession());
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("isSuccess", true);
                } else {
                    map.put("code", 200);
                    map.put("msg", "密码或用户名有误,请重新登录!");
                    map.put("isSuccess", false);
                }
            } else {
                map.put("code", 0);
                map.put("msg", "当前是登录状态");
                map.put("isSuccess", false);
            }
        }
        return map;
    }



    @RequestMapping("/register")
    @ResponseBody
    public Map<String, Object> register(HttpServletRequest request) {
        SysUser user = WebUtil.getParameter(request, SysUser.class);
        String algorithmName = "MD5";
        Object soure = user.getPassword();
        Object salt = ByteSource.Util.bytes(user.getUserName());
        Integer hashIterations = 1024;
        String pwd = (new SimpleHash(algorithmName,soure,salt,hashIterations)).toString();
        user.setPassword(pwd);
        SysUser creat = SysUserOperate.getUser();
        if (creat != null) {
            user.setCreateBy(creat.getId());
        }
        Map<String,Object> map = apiService.insert(user);
        return map;
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Map<String,Object> deleteUser(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        SysUser sysUser = SysUserOperate.getUser();
        SysUser user = WebUtil.getParameter(request, SysUser.class);
        if (sysUser != null && sysUser.getUserName().equals("admin")) {
            sysUserMapper.delete(new EntityWrapper<SysUser>().where("id_={0}", user.getId()));
            map.put("msg", "删除成功");
            map.put("code", 200);
            map.put("isSuccess", true);
        } else {
            map.put("msg", "非管理员禁止操作");
            map.put("code", 200);
            map.put("isSuccess", false);
        }
        return map;



    }

    @RequestMapping("/logout")
    @ResponseBody
    public boolean logout(){
        SysUserOperate.logout();
        return true;
    }


    @RequestMapping("/getUserList")
    @ResponseBody
    public List<SysUser> getUserList(HttpServletRequest request, Integer pageSize, Integer pageNum) {
        SysUser user = SysUserOperate.getUser();
        pageNum = 1;
        pageSize = 10000;
        Page<SysUser> page =null;
        if (pageSize != null&&pageNum != null) {
            page = new Page<>(pageNum,pageSize);
        }
        if (page == null) {
            page = new Page<>();
        }
        if (!user.getUserName().equals("admin")) {
            return  sysUserMapper.selectPage(page, new EntityWrapper<SysUser>().where("user_name={0}", user.getUserName()));
        } else {
            return sysUserMapper.selectPage(page, new EntityWrapper<>());
        }
    }






}
